//
//  TF_informationCollectionView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2023/8/28.
//  Copyright © 2023 Talkfun. All rights reserved.
//

import UIKit

class TF_informationCollectionView: TalkfunHitTestView , UITableViewDelegate,UITableViewDataSource,UITextViewDelegate {
    
    let livingSDK = TalkfunSDKLifeLive.sharedInstance()
    
    let view = UIButton()
    var viewHight:CGFloat = 390
    
    let title = UILabel()
    let titleRemark = UILabel()
    
    let line = UIView()
    let bottom = UIView()
    let submit = UIButton()
    
    let tableView = UITableView()
    var list:[TFQuestionnaireItems] = []
    let noneView = UIView()
    
    //提交用的
    var submitdict:[String:String] = [:]
    
    //是否点击过提交
    var isSubmitClick:Bool = false
    
  
    //提交成功
    var submittedCode:Bool = false
    //键盘是否在显示
    var isKeyboardWillShow:Bool = false
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        
        self.addSubview(self.view)
        self.view.addTarget(self, action: #selector(viewClick), for: .touchUpInside)
        self.view.backgroundColor = .white
        self.view.frame = CGRect(x: 0, y: self.frame.height, width: self.frame.width, height: self.viewHight)
        
        
        self.view.tf_roundedCorners(corners: [.topLeft,.topRight], cornerRadii: CGSize(width: 12, height: 12))
        
        
        self.view.addSubview(title)
        title.font = UIFont.PingFang(size: 16)
        title.textAlignment = .center
        title.frame = CGRect(x: 0, y: 11, width: self.frame.width, height: 22.5)
        //-------------
        self.view.addSubview(titleRemark)
        titleRemark.textAlignment = .center
        titleRemark.frame = CGRect(x: 0, y: title.frame.maxY+3, width: self.frame.width, height: 20)
        //------------
        line.backgroundColor = UIColor(red: 0.92, green: 0.94, blue: 0.95, alpha: 1)
        view.addSubview(line)
        //---------------
        view.addSubview(bottom)
        self.bottom.frame = CGRect(x: 0, y: self.view.frame.height-70, width: self.frame.width, height: 70)
        self.bottom.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
        self.bottom.layer.shadowColor = UIColor(red: 0.83, green: 0.81, blue: 0.85, alpha: 0.4).cgColor //阴影颜色
        self.bottom.layer.shadowOffset = CGSize(width: 0, height: 0)//阴影偏移
        self.bottom.layer.shadowOpacity = 1 //阴影不透明度
        self.bottom.layer.shadowRadius = 10 //阴影半径
        //------------------------
        bottom.addSubview(submit)
        let submitW = bottom.frame.width-25-25
        let submitH = bottom.frame.height-13-13
        let submitX = 25.0
        let submitY = 13.0
        submit.frame = CGRect(x: submitX, y: submitY, width: submitW, height: submitH)
        //蓝色  所有选项已经填写
        submit.backgroundColor = UIColor(red: 0, green: 0.76, blue: 1, alpha: 1)
        //灰色  还有选项未填写
//        submit.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
        
        submit.setTitle("提交", for: .normal)
        submit.setTitleColor(.white, for: .normal)
        submit.titleLabel?.font = UIFont.PingFang(size: 18)
        submit.layer.cornerRadius = 4
        submit.layer.masksToBounds = true
        submit.addTarget(self, action: #selector(submitClick), for: .touchUpInside)

        
        self.tableView.backgroundColor = UIColor.clear
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.view.addSubview(self.tableView)
        self.tableView.register(TF_informationCollectionViewCell.self, forCellReuseIdentifier: "TF_informationCollectionViewCell")
        
        self.tableView.separatorStyle = .none;
        tableView.backgroundColor = .white
        //---------
        noneView.backgroundColor = .white
//------------------
        
        //键盘显示前
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(noti:)), name: UIResponder.keyboardWillShowNotification, object: nil)

        //键盘隐藏前
        NotificationCenter.default.addObserver(self, selector: #selector(self.KeyboardWillHide(noti:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }

    //键盘显示前
    @objc func keyboardWillShow(noti:NSNotification) {
        
        if UIResponder.tf_firstResponder() == nil {
            return
        }
        let firstResponder:UIView = UIResponder.tf_firstResponder() as! UIView
        if let aa = firstResponder.superview
        {
            if let bb = aa.superview
            {
                if(bb is TF_informationCollectionViewCell)
                {
                    
                    self.isKeyboardWillShow = true
                    let keyboardInfo:NSDictionary = noti.userInfo! as NSDictionary
                    
                    let keyboardFrameEnd:NSValue = keyboardInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
                    
                    let keyboardFrameEndRect:CGRect = keyboardFrameEnd.cgRectValue

                    let height = keyboardFrameEndRect.size.height
                    
                    
                    
                    self.view.frame = CGRect(x: 0, y: self.frame.size.height - height - self.viewHight, width: self.view.frame.size.width, height: self.viewHight)
                }
                
            }
         
           
        }
        
    }
    @objc public func destroySharedIntance()
    {
        NotificationCenter.default.removeObserver(self)
        if(TF_informationCollectionViewCell.shared.timer != nil)
        {
            TF_informationCollectionViewCell.shared.timer?.invalidate()
            TF_informationCollectionViewCell.shared.timer = nil
        }

    }
   //隐藏了键盘
    @objc func KeyboardWillHide(noti:NSNotification) {
        
        if UIResponder.tf_firstResponder() == nil {
            return
        }
        let firstResponder:UIView = UIResponder.tf_firstResponder() as! UIView
        if let aa = firstResponder.superview
        {
            if let bb = aa.superview
            {
                if(bb is TF_informationCollectionViewCell)
                {
                    self.isKeyboardWillShow = false
                    self.view.frame = CGRect(x: 0, y: self.frame.height-self.viewHight, width: self.frame.width, height: self.viewHight)
      
                }
                
            }
         
           
        }
        
     
        
    }
    @objc func viewClick()
    {
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        

    }

    @objc func isShow(_ isShow:Bool)
    {
        if(self.superview != nil)
        {
            self.superview!.addSubview(self)
        }
        UIView.animate(withDuration: 0.6) {
            if (isShow) {
                let viewY = self.frame.height-self.viewHight
                self.view.frame = CGRect(x: 0, y:viewY , width: self.frame.width, height: self.viewHight)


             }else{
                 self.view.frame = CGRect(x: 0, y: self.frame.height, width: self.frame.width, height: self.viewHight)
               

             }
        }

    }
    
    //TODO: 初始化list数据
    func setModelUI(_ model:TFQuestionnaire)
    {

        self.title.attributedText = NSMutableAttributedString.getAttributed(string: model.title.theme, font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 0.14, green: 0.22, blue: 0.25,alpha:1))
        
        
        titleRemark.attributedText = NSMutableAttributedString.getAttributed(string: model.title.content, font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 0.59, green: 0.61, blue: 0.62,alpha:1))
        
        //-----------
        if(model.title.content.count==0)
        {
            line.frame = CGRect(x: 0, y: title.frame.maxY+10.5, width: self.frame.width, height: 0.5)
        }else
        {
            line.frame = CGRect(x: 0, y: titleRemark.frame.maxY+9, width: self.frame.width, height: 0.5)
        }
        let tableviewY = line.frame.maxY
        let tableviewH = bottom.frame.origin.y-tableviewY
        tableView.frame = CGRect(x: 0, y: tableviewY, width: self.frame.width, height: tableviewH)

        //-------------------------------
        self.list.removeAll()
        
        for i in 0..<model.items.count {
            let new = model.items[i] as! TFQuestionnaireItems
            self.list.append(new)
        }
        
        self.submitdict.removeAll()
        
        self.tableView.reloadData()
        self.submit.isUserInteractionEnabled = true
        //TODO:  获取已填写信息采集(问卷)信息，未填写过信息采集返回空数据
        self.livingSDK.getFilledQuestionnaire {[weak self] code, model,error in
            guard let `self` = self else { return }
            
            if(code==0)
            {
               
                let items2 = model.info.items
                if(items2.count>0)
                {
                    //灰色  还有选项未填写
                    self.submit.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
                    self.submit.isUserInteractionEnabled = false
                    self.submit.setTitle("已提交", for: .normal)
                    self.submittedCode = true
                }else
                {
                    //蓝色  所有选项已经填写
                    self.submit.backgroundColor = UIColor(red: 0, green: 0.76, blue: 1, alpha: 1)
                    self.submit.isUserInteractionEnabled = true
                    self.submit.setTitle("提交", for: .normal)
                    self.submittedCode = false
                }
                
      
                for i in 0..<items2.count {
                    
                    let newModel = items2[i] as! TFQuestionnaireItems
                    
                    //必要项  边框色
                    
                    let index = String(format: "%i",i)
                    
                     var key = "collect_" + index
                    self.submitdict[key] = newModel.value
                     //验证码
                     if(newModel.verification==1)
                     {
                        key = key + "_check"
                         //后端没有返回
//                         self.submitdict[key] = newModel.value
                         
                      }
                    
                
                }
                
                self.tableView.reloadData()
           
                
            }else
            {
                
                
            }
        }
    }
    //穿透
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01 ) {
            return nil
        }

        
        // 2.判断下点在不在当前控件上
        // 用pointInside方法判断点在不在控件上,point这个必须是方法调用者坐标系上的点
        if (self.point(inside: point, with: event) == false){
            return nil
        }
        // 3.从后往前遍历自己的子控件
        
        let count = self.subviews.count
        
        //倒序
        for i in (0..<count).reversed() {

        // 取出子控件
        let childV = self.subviews[i] as UIView
            
        // 把自己坐标系上点转化成子控件坐标系上点
          let  childP =  self.convert(point, to: childV)
          let fitView =  childV.hitTest(childP, with: event)
             
            // 如果找到最合适view就直接返回
            if ((fitView) != nil) {
                return fitView;
            }
            
        }

//        if(self.isKeyboardWillShow)
//        {
//            self.endEditing(true)
//        }
//        else
//        {
//
//            if(self.view.frame.origin.y==self.frame.height)
//            {
//
//
//            }else
//            {
                self.isShow(false)
//            }
//
//        }
        return nil

    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.list.count
    }
    //TODO: 创建cell,此时cell是可选类型
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "TF_informationCollectionViewCell") as! TF_informationCollectionViewCell
        cell.selectionStyle = .none
        //用于提交的下标
        cell.content.tag = indexPath.row
        cell.verificationCodeContent.tag = indexPath.row
        
        cell.cellW = self.frame.width
        let model = self.list[indexPath.row]
        cell.setUI(model,submittedCode:self.submittedCode)
        
        let index = String(format: "%i", indexPath.row)
        var key = "collect_" + index
      
        if let value = submitdict[key]
        {
            cell.content.text = value
        }else
        {
            cell.content.text = ""
        }
        cell.content.keyboardType = .default
        //验证码
        if(model.verification==1)
        {
            cell.content.keyboardType = .numberPad
            
            key = key + "_check"
            
            if let value = submitdict[key]
            {
                cell.verificationCodeContent.text = value

            }else
            {
                cell.verificationCodeContent.text = ""
            }
        }
        
      //UITextViewDelegate
        cell.content.delegate = self
        cell.verificationCodeContent.delegate = self
        
        self.setBorderColorUI(cell: cell, model: model)

        cell.isUserInteractionEnabled = !self.submittedCode
        //已经提交
        if(self.submittedCode == true)
        {
            let obtainTitleAttributedText = NSMutableAttributedString.getAttributed(string: "获取验证码", font: UIFont.PingFang(size: 16), foregroundColor:UIColor(red: 0.2, green: 0.5, blue: 1, alpha: 1))
            cell.obtainTitle.attributedText = obtainTitleAttributedText
            cell.obtainTitle.isHidden = true
        }
       
        return cell
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 15 // 设置底部空间的高度为 15 点
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return self.noneView // 创建一个空的 UIView 作为底部空间
    }
 
    // 选中cell后执行此方法
       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           
           tableView.deselectRow(at: indexPath, animated: true)
           
       }
    
    //UITextViewDelegate
    func textViewDidBeginEditing(_ textView: UITextView) {
        // Calculate the cell's frame in the UITableView's coordinate space
        let textViewFrameInTableView = textView.convert(textView.bounds, to: self.tableView)
        
        // Calculate the amount to adjust the contentOffset by
        let offsetY = textViewFrameInTableView.minY // You can adjust this value
        
        // Update the contentOffset of the table view
        
        self.tableView.setContentOffset(CGPoint(x: 0, y: offsetY-42.5), animated: false)
        
    }
    // 设置cell高度
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            
            let model = self.list[indexPath.row]
            
            var cellH = 42.5+44.0
            //留言板
            if(model.type==4)
            {
                cellH = 42.5+100.0
            }else if(model.type==2 && model.verification==1)
            {  //
                
                cellH = (42.5+44.0)*2
                
            }
            
            return cellH
        }
    

    //TODO: 提交收集的内容
    @objc func submitClick()
    {
        self.isSubmitClick = true
        
        print("收集的内容：\(self.submitdict)")
        
        self.tableView.reloadData()
        
        //必选项
        var show:Bool = false
        for i in 0..<self.list.count {
           let model = self.list[i]
            //必选项
            if(model.checked==1 && model.type != 2)
            {
                //必要项  边框色
                let index = String(format: "%i", i)

                 let key = "collect_" + index
                
                if let value = submitdict[key]
                {
                    
//                    //验证码
//                     if(model.verification==1)
//                     {
//                         let check = key + "_check"
//                         if let checkValue = submitdict[check]
//                         {
//
//                         }else
//                         {
//                             show = true
//                         }
//
//                     }
                    
                }else
                {  //未填写
                    show = true
                }
                
            }else     if(model.type==2)

            {   //类型是手机号
                //必要项  边框色
                let index = String(format: "%i", i)

                 let key = "collect_" + index
                
                if let value = submitdict[key]
                {
                    
                    //验证码
                     if(model.verification==1)
                     {
                         let check = key + "_check"
                         if let checkValue = submitdict[check]
                         {
                             
                         }else
                         {
                             show = true
                             self.view.toast("请输入验证码", position: CGPoint(x: UIScreen.main.bounds.width/2, y: self.view.frame.height/2))
                             return
                         }
                         
                     }
                    
                }
            }
            
        }
        
        if(show)
        {
            
            self.view.toast("请完善信息", position: CGPoint(x: UIScreen.main.bounds.width/2, y: self.view.frame.height/2))
            
        }else
        { //可以提交数据了
         
//收集的内容：["collect_1": "2", "collect_0": "1", "collect_2_check": "9352", "collect_2": "15622158698"]
            self.livingSDK.submitQuestionnaire(submitdict) {[weak self] code, error in
                guard let `self` = self else { return }
                if(code==0)
                {
                    self.view.toast("提交成功", position: CGPoint(x: UIScreen.main.bounds.width/2, y: self.view.frame.height/2))
                    //灰色
                    self.submit.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
                    self.submit.isUserInteractionEnabled = false
                    self.submittedCode = true
                    if(TF_informationCollectionViewCell.shared.timer != nil)
                    {
                        TF_informationCollectionViewCell.shared.timer?.invalidate()
                        TF_informationCollectionViewCell.shared.timer = nil
                    }
                    self.tableView.reloadData()
                }else
                {
                    self.view.toast(error?.msg, position: CGPoint(x: UIScreen.main.bounds.width/2, y: self.view.frame.height/2))
                    
                }
                
            }
            
        }
        
    }
    // UITextViewDelegate 方法，文本发生变化时被调用
     func textViewDidChange(_ textView: UITextView) {
         // 这里可以获取输入的文本内容，并对其进行处理
         if let text = textView.text {
//             print("输入的文本内容：\(text)")
             
             
             if(textView.tag < self.list.count)
             {
                 let model = self.list[textView.tag]
    
             if(textView.tag < self.list.count)
             {

                 
                 let indexPath = IndexPath(row: textView.tag, section: 0)
                 if let cell = tableView.cellForRow(at: indexPath) {
                     // 在这里你可以使用 `cell` 进行操作
                     
                     if(cell is TF_informationCollectionViewCell)
                     {
                         if let cell = cell as? TF_informationCollectionViewCell {
                             //必要项  边框色
                             let index = String(format: "%i", textView.tag)
                             
                              var key = "collect_" + index
                             //验证码 开启
                             if(model.verification==1)
                             {
                                 if(textView == cell.verificationCodeContent)
                                 {
                                     key = key + "_check"
                                 }else
                                 {  //输入号码
                                     
                                     
                                     
                                     TF_informationCollectionViewCell.shared.phone = text
                                     
                                 }
                                     
                                
                             }
                             

                             if(text.count==0)
                             {
                                 self.submitdict.removeValue(forKey: key)
                             }else
                             {
                                 self.submitdict[key] = text
                             }
                            self.setBorderColorUI(cell: cell, model: model)

                         }
                     }

                 } else {
                     print("Cell at indexPath \(indexPath) not found.")
                 }
             }

             }
             
         }
     }

    func setBorderColorUI(cell:TF_informationCollectionViewCell,model:TFQuestionnaireItems)
    {
        cell.content.layer.borderColor = UIColor.clear.cgColor
        cell.verificationCodeContent.layer.borderColor = UIColor.clear.cgColor
        if(self.isSubmitClick)
        {

            //checked 必选项
            if(model.checked==1)
            {
                if let text = cell.content.text
                {
                    if(text.count==0)
                    {
                        cell.content.layer.borderColor = UIColor.red.cgColor
                    }
                   
                }
              
                if let text = cell.verificationCodeContent.text
                {
                    if(text.count==0)
                    {
                        cell.verificationCodeContent.layer.borderColor = UIColor.red.cgColor
                    }
                    
                }
              
            }
        }
    }
}



class  TF_informationCollectionViewCell: UITableViewCell
{

    
    let title = UILabel()
    let content = TF_TextView()
    var cellW:CGFloat = 0

    //验证码标题
    let verificationCodeTitle = UILabel()
    //获取验证码
    let obtainTitle = UILabel()
    //请填写验证码
    let verificationCodeContent = TF_TextView()
    
    var isAddGestureRecognizer:Bool = false
    var timer:Timer?
    
    //默认60秒倒计时
    var duration:NSInteger = 60
    //手机号码
    var phone:String = ""
    static let shared = TF_informationCollectionViewCell()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
   

        self.contentView.addSubview(title)
        
        self.contentView.addSubview(content)
        content.backgroundColor = UIColor(red: 0.98, green: 0.98, blue: 0.98, alpha: 1)
        content.textContainerInset = UIEdgeInsets(top: 10, left: 11, bottom: 0, right: 0)
        content.font = UIFont.PingFang(size: 16)
        content.layer.borderWidth = 0.5
        content.layer.masksToBounds = true
        
        content.tf_placeholderX = 16
        content.tf_placeholderY = 10
        //灰色
        content.tf_placeholderColor = UIColor(red: 0.85, green: 0.87, blue: 0.89,alpha:1)
        //----------
        self.contentView.addSubview(obtainTitle)
        let obtainTitleAttributedText = NSMutableAttributedString.getAttributed(string: "获取验证码", font: UIFont.PingFang(size: 16), foregroundColor:UIColor(red: 0.2, green: 0.5, blue: 1, alpha: 1))
        obtainTitle.attributedText = obtainTitleAttributedText
        obtainTitle.textAlignment = .right
        //-------------------------------
        let titleAttributedText = NSMutableAttributedString.getAttributed(string: "短信验证", font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 0.4, green: 0.43, blue: 0.44,alpha:1))
        self.contentView.addSubview(verificationCodeTitle)
        verificationCodeTitle.attributedText = titleAttributedText
        
        
        
        self.contentView.addSubview(verificationCodeContent)
        verificationCodeContent.backgroundColor = UIColor(red: 0.98, green: 0.98, blue: 0.98, alpha: 1)
        verificationCodeContent.textContainerInset = UIEdgeInsets(top: 10, left: 11, bottom: 0, right: 0)
        verificationCodeContent.font = UIFont.PingFang(size: 16)
        verificationCodeContent.layer.borderWidth = 0.5
        verificationCodeContent.layer.masksToBounds = true
        
        verificationCodeContent.tf_placeholderX = 16
        verificationCodeContent.tf_placeholderY = 10
        //灰色
        verificationCodeContent.tf_placeholderColor = UIColor(red: 0.85, green: 0.87, blue: 0.89,alpha:1)
        //限定是数字
        verificationCodeContent.keyboardType = .numberPad

        
        verificationCodeTitle.isHidden = true
        verificationCodeContent.isHidden = true
        obtainTitle.isHidden = true
        obtainTitle.isUserInteractionEnabled = true
        //点击手势
        self.addObtainTitleGestureRecognizer(target: self, action: #selector(obtainTitleClick))
    }
    func addObtainTitleGestureRecognizer(target: Any?, action: Selector)
    {
        if(self.isAddGestureRecognizer==false)
        {
            isAddGestureRecognizer = true
            let tapGesture = UITapGestureRecognizer(target: target, action: action)
            obtainTitle.addGestureRecognizer(tapGesture)
        }

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setUI(_ model:TFQuestionnaireItems,submittedCode:Bool)
    {
     
        
        verificationCodeTitle.isHidden = true
        verificationCodeContent.isHidden = true
        obtainTitle.isHidden = true
        let titleX = 15.0
        title.frame = CGRect(x: titleX, y: 15.0, width: self.cellW-titleX-titleX, height: 22.5)
        
        let contentX = 25.0
        let contentY = 42.5
        var contentH = 44.0
        
        //留言板
        if(model.type==4)
        {
            contentH = 100.0
        }else if(model.type==2 && model.verification==1)
        {  //手机号输入 && 验证码输入
          
            verificationCodeTitle.isHidden = false
            verificationCodeContent.isHidden = false
            if(submittedCode == false)
            {
                obtainTitle.isHidden = false
            }else
            {
                obtainTitle.isHidden = true
            }
            
        }
        content.frame = CGRect(x: contentX, y: contentY, width: cellW-contentX-contentX, height:contentH )
        
        
        //---------------------
        verificationCodeTitle.frame = CGRect(x: titleX, y: content.frame.maxY+15.0, width: self.cellW-titleX-titleX, height: 22.5)
        obtainTitle.frame = verificationCodeTitle.frame
        
        verificationCodeContent.frame = CGRect(x: contentX, y: verificationCodeTitle.frame.maxY+5.0, width: cellW-contentX-contentX, height:44.0)
        //--------------------------------
        let titleAttributedText = NSMutableAttributedString.getAttributed(string: model.title, font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 0.4, green: 0.43, blue: 0.44,alpha:1))
        
    
        
        //必选 *
        if(model.checked==1)
        {
            let attributedString2 = NSMutableAttributedString.getAttributed(string: "*", font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 1, green: 0.34, blue: 0.37,alpha:1))
            
            titleAttributedText.append(attributedString2)

        }
        title.attributedText = titleAttributedText
        //-----------------
        content.tf_placeholder = model.descriptionMsg
        verificationCodeContent.tf_placeholder = "请填写验证码"
    }
    func isNumeric(_ inputStr: String) -> Bool {
        // 创建一个CharacterSet，表示数字字符集合
        let digitCharacterSet = CharacterSet.decimalDigits
        
        // 创建一个Scanner对象，用于扫描字符串
        let scanner = Scanner(string: inputStr)
        
        // 扫描字符串，查看是否只包含数字字符
        var scannedInt: Int = 0
        return scanner.scanInt(&scannedInt) && scanner.isAtEnd
    }

    //TODO: 获取手机号码的验证码
    @objc func obtainTitleClick(label:UILabel)
    {
        if TF_informationCollectionViewCell.shared.timer == nil {

            let phone = TF_informationCollectionViewCell.shared.phone
            
            if(phone.count==11 && self.isNumeric(phone))
            {
        
                let livingSDK = TalkfunSDKLifeLive.sharedInstance()
                livingSDK.sendSMSVerificationCode(phone) {[weak self] code, error in
                    guard let `self` = self else { return }
        
                    if(code==0)
                    {
//                        print("phone=====>",phone);
                        TF_informationCollectionViewCell.shared.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
                        TF_informationCollectionViewCell.shared.timer?.fire()
                        RunLoop.main.add(TF_informationCollectionViewCell.shared.timer! , forMode: RunLoop.Mode.default)
        
        
                    }else
                    {
                        self.toast(error?.msg, position: CGPoint(x: self.bounds.width/2, y: self.bounds.height/2))
                       
                    }
        
                }
            }else
            {
                self.toast("请求的参数错误", position: CGPoint(x: self.bounds.width/2, y: self.bounds.height/2))
            }
  
        }
        
    }
    
   @objc func timerAction()
    {
        
       
        
       let durationStr = String(format: "%is", self.duration)
        let obtainTitleAttributedText = NSMutableAttributedString.getAttributed(string: durationStr, font: UIFont.PingFang(size: 16), foregroundColor:UIColor(red: 0.2, green: 0.5, blue: 1, alpha: 1))
        obtainTitle.attributedText = obtainTitleAttributedText
        self.duration = self.duration-1
        if(self.duration==0)
        {
            if TF_informationCollectionViewCell.shared.timer != nil {
                TF_informationCollectionViewCell.shared.timer?.invalidate()
                TF_informationCollectionViewCell.shared.timer = nil
            }
            let obtainTitleAttributedText = NSMutableAttributedString.getAttributed(string: "获取验证码", font: UIFont.PingFang(size: 16), foregroundColor:UIColor(red: 0.2, green: 0.5, blue: 1, alpha: 1))
            obtainTitle.attributedText = obtainTitleAttributedText
            self.duration = 60
        }
        
    }
}
